<?php
class Login{
	const LOGIN_USER_UNKNOWN = 1;
	const LOGIN_WRONG_PASS = 2;
	const LOGIN_ACCESS = 3;

	private $observers;
	private $status = array();

	function handleLogin($user,$pass,$ip){
		$random = rand(1,3);
		echo $random."\n<BR/>";
		switch($random){
			case 1:
				$this->setStatus(self::LOGIN_ACCESS,$user,$ip);
				$ret = true;
				break;
			case 2:
				$this->setStatus(self::LOGIN_WRONG_PASS,$user,$ip);
				$ret = false;
				break;
			case 3:
				$this->setStatus(self::LOGIN_USER_UNKNOWN,$user,$ip);
				$ret = false;
				break;
		}
		$this->notify();
		return $ret;
	}

	private function setStatus($status,$user,$ip){
		$this->status = array($status,$user,$ip);
	}

	function getStatus(){
		return $this->status;
	}


	function attach(Observer $observer){
		$this->observers[] = $observer;
	}

	function detach(Observer $observer){
		$newobservers = array();
		foreach($this->observers as $obs){
			if($obs !== $observer){
				$newobservers[] = $obs;
			}
		}
		$this->observers = $newobservers;
	}

	function notify(){
		foreach($this->observers as $obs){
			$obs->update($this);
		}
	}
}